package bag.dao;

import bag.config.C3P0Utils;
import bag.domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * Created by 冷 on 2017/9/30.
 */
public  class UserDao {
    public User findUsernameAndPassword(String uname, String pword) throws SQLException {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSources());
        String sql = "select * from user where username = ? and password = ?";
        User user = (User) runner.query(sql,new BeanHandler<>(User.class),uname,pword);
        return user;
    }
    public boolean addUsernameAndPassword(User user) throws SQLException {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSources());
        String sql = "insert into user values(,'?','?')";
        int num =  runner.update(sql,new Object[]{user.getUsername(),user.getPassword()});
        if(num>0)
        {
            return true;
        }
        return false;
    }

}
